/*
Copyright 2008, Mark Arvidson

This file is part of EwaScorecard.

EwaScorecard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

EwaScorecard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with EwaScorecard.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.microedition.lcdui.*;

public class CellPainter {
    
    public static final int FIRST_COLOR = 0xF00000;
    public static final int SECOND_COLOR = 0xB00000;
    public static final int THIRD_COLOR = 0x700000;
    public static final int DEFAULT_COLOR = 0xFFFFFF;

    private TeamData teamData;
    private int i; // row

    private int j; // column

    public static int cellWidth;
    public static int cellHeight;
    public static int topCell;
    public static int leftCell;
    public static int leftMostColumn;
    public static int topMostRow;

    public CellPainter(TeamData teamData, int i, int j) {
        this.teamData = teamData;
        this.i = i;
        this.j = j;
    }

    public int getRow() {
        return i;
    }

    public int getColumn() {
        return j;
    }

    public boolean isHit(int x, int y) {
        return i - topMostRow == (y - topCell) / cellHeight &&
                j - leftMostColumn == (x - leftCell) / cellWidth;
    }

    public void paint(Graphics g, int selectedRow, int selectedColumn) {
        // fill boxes for top scores
        int fillColor = DEFAULT_COLOR;
        int textColor = 0x000000;
        int dispRow = i - topMostRow;
        int dispCol = j - leftMostColumn;
        if (dispRow >= 0 && dispCol >= 0) {
            switch (teamData.getScorePlacement(i, j)) {
                case 1:
                    fillColor = FIRST_COLOR;
                    textColor = 0xFFFFFF;
                    break;
                case 2:
                    fillColor = SECOND_COLOR;
                    textColor = 0xFFFFFF;
                    break;
                case 3:
                    fillColor = THIRD_COLOR;
                    textColor = 0xFFFFFF;
                    break;
            }
            //if (fillColor != 0xFFFFFF) {
            g.setColor(fillColor);
            g.fillRect((dispCol * cellWidth) + leftCell + 1, (dispRow + 1) * cellHeight + 1, cellWidth - 1, cellHeight - 1);
            g.setColor(textColor);
            //}

            boolean aaCol = j == teamData.getNumberOfEvents();
            g.drawString(formatFloat(teamData.rowData(i, j), aaCol ? 2 : 3),
                    ((dispCol + 1) * cellWidth) + leftCell - 2,
                    ((dispRow + 1) * cellHeight) + 2, Graphics.TOP | Graphics.RIGHT);
            // selected cell has a dotted border inside the normal border
            if (selectedRow == i && selectedColumn == j) {
                g.setStrokeStyle(Graphics.DOTTED);
                g.drawRect((dispCol * cellWidth) + leftCell + 1, (dispRow + 1) * cellHeight + 1, cellWidth - 2, cellHeight - 2);
                g.setStrokeStyle(Graphics.SOLID);
            }
            g.setColor(0x000000); // black by default        

        }
    }

    public static String formatFloat(float value, int places) {
        float epsilon = 0.5f;
        for(int counter = 0; counter < places; counter++) {
            epsilon /= 10;
        }
        value += epsilon;
        StringBuffer result = new StringBuffer();
        int whole = (int) value;
        result.append(whole);
        result.append('.');
        int offset = 1;
        for (int counter = 0; counter < places; counter++) {
            offset *= 10;
        }
        String fraction = String.valueOf((int) ((value - whole) * offset));
        while (fraction.length() < places) {
            fraction = "0" + fraction;
        }
        result.append(fraction);
        return result.toString();
    }
}
